require 'json'

class Email
  def initialize(email = 'abc@xyz.com', description = 'the description', suppress = 0, suppress_reason = 'suppress reason', is_deleted = rand(2))
    @email = email
    @description = description
    @suppress = suppress
    @suppress_reason = suppress_reason
    @is_deleted = is_deleted
    true
  end
  attr_accessor :email, :description, :suppress, :suppress_reason, :is_deleted
end

class Phone
    def initialize(number = '1234' , description = 'description', type = 'type', suppress = nil, suppress_reason = nil, is_deleted = 0)
    @phone = number
    @description = description
    @type = type
    @suppress = suppress
    @suppress_reason = suppress_reason
    @is_deleted = is_deleted
  end
  attr_reader :phone, :description, :type, :suppress, :suppress_reason, :is_deleted
end

#~ p Email.new(:email => 'abc@xyz.com', :description => 'description', :suppress => 0, :suppress_reason => 'Suppress Reason', :is_deleted => 1)

  phones = []
  10.times do
    
    phones << Phone.new
  end

  def objarr_to_hash(objarr, escape_attr = nil, attr_condition = nil)
    arr = []
    for i in 0...objarr.size
      attr_condition ? (arr << obj_to_hash(objarr[i], escape_attr) if eval("objarr[i].#{attr_condition}")) : (arr << obj_to_hash(objarr[i], escape_attr))
    end
    arr
  end
    
  def obj_to_hash(obj, escape_attr)
    variables = obj.instance_variables
    hash = Hash.new
    for i in 0...variables.size
      variables[i] = strip_at(variables[i])
      escape_attr ? (hash.store(variables[i], eval("obj.#{variables[i]}")) if not escape_attr.include?(variables[i])) : hash.store(variables[i], eval("obj.#{variables[i]}"))
    end
    hash
  end

  def strip_at(string)
    string = string.to_s
    return string.delete('@') if string.class.to_s == 'String' && string.include?('@')
    string
  end
  
#~ p objarr_to_hash(phones, 'is_deleted == 0')
#~ p objarr_to_hash(emails,['suppress', 'suppress_reason', 'is_deleted','description'],'is_deleted == 0').to_json

def last_string(str, no)
  final = ''
  len = str.size
  if no < len + 1
    for i in 0...no
      index = (len - no) + i
      final = final + str[index].chr
    end
  else  
    raise 'Last character index passed is greator than length of the string'
  end
  final
end



#~ p last_string('chirantan',10)